#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iostream>
#include <string.h>
#include "common.h"
#include "log.hpp"

int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        std::cout << "Usage:\n\t" << argv[0] << " serverip serverport" << std::endl;
    }
    uint16_t serverport = std::stoi(argv[2]);
    std::string serverip = argv[1];

    // 填充服务器地址结构体信息
    sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(serverport);
    server.sin_addr.s_addr = inet_addr(serverip.c_str());

    // 创建套接字
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd < 0) exit(sockError);

    // 与服务器建立连接
    if(connect(sockfd, Convert(&server), sizeof(server)) != 0)
    {
        lg.LogMessage(Fatal, "connect socket failed");
        exit(connectError);
    }
    lg.LogMessage(Debug, "connect to server success");

    char service_list[1024];
    int m = read(sockfd, service_list, sizeof(service_list) - 1);
    service_list[m] = 0;
    std::cout << "service list:\n\t" << service_list << std::endl;

}